package com.link.play.fit.ai.ui.weightcontrol.ac

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.store.CloudStore.DOC_USER_GUIDE
import com.link.play.fit.ai.utils.generateBMRFemale
import com.link.play.fit.ai.utils.generateBMRMale
import com.link.play.fit.ai.utils.getCarbsByCal
import com.link.play.fit.ai.utils.getFatsByCal
import com.link.play.fit.ai.utils.getProteinByCal
import com.link.play.fit.ai.utils.kgCal
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt

class WeighControlViewModel() : ViewModel() {

    private val TAG = "WeighControlViewModel"
    var userGuideState: MutableStateFlow<UserGuide> = MutableStateFlow(UserGuide()) //状态
    var lastUserGuide = UserGuide()//最新值/临时值

    init {
        viewModelScope.launch {
            val userGuide = CloudStore.get<UserGuide>(DOC_USER_GUIDE)
            userGuide?.let { old ->
                userGuideState.update { old }
            }
        }
    }

    private fun getBMR() {
        val data: UserGuide = lastUserGuide
        val exerciseRate = when (data.workouts) {
            0 -> 1.0
            1 -> 1.2
            2 -> 1.375
            else -> {
                1.0
            }
        }
        val bmr = if (data.gender == 0)
            (generateBMRMale(data.weightKg, data.heightCm, data.age) * exerciseRate).roundToInt()
        else
            (generateBMRFemale(data.weightKg, data.heightCm, data.age) * exerciseRate).roundToInt()
        data.bmr = bmr
    }

    /**
     * 算lastUserGuide的卡路里
     */
    fun getCalculateLastDaily() {
        getBMR()
        customDailyPlan()
    }

    /**
     * 注意这里是 目标天数30天；目标体重  计算的每日目标
     */
    private fun customDailyPlan() {

        val data = lastUserGuide

        var caloriesDaily = 0
        var proteinDaily = 0
        var fatsDaily = 0
        var carbsDaily = 0

        var weightChangeDaily = abs(data.goalWeightKg - data.weightKg) / 30f
        if (data.goal == 2) {
            //0.014-0.2142
            weightChangeDaily = min(weightChangeDaily, 0.1428f)
        }
        Log.e(TAG, "goal=${data.goal}")
        Log.e(TAG, "weightChangeDaily=$weightChangeDaily")

        val dailyCal = weightChangeDaily * kgCal

        val action = {
            val d = if (data.gender == 0) 1.0f else 0.75f
            proteinDaily = getProteinByCal(data.weightLbs, data.caloriesDaily, d)
            fatsDaily = getFatsByCal(caloriesDaily)
            carbsDaily = getCarbsByCal(caloriesDaily, proteinDaily, fatsDaily)
        }
        when (data.goal) {
            0 -> {
                caloriesDaily = data.bmr + dailyCal.roundToInt()
                action.invoke()
            }

            1 -> {
                caloriesDaily = data.bmr
                action.invoke()
            }

            2 -> {
                caloriesDaily = data.bmr - dailyCal.roundToInt()
                caloriesDaily = max(caloriesDaily, 1200)
                action.invoke()
            }
        }

        Log.e(
            TAG,
            "data.bmr=${data.bmr} " +
                    "dailyCal=$dailyCal " +
                    "caloriesDaily=$caloriesDaily " +
                    "proteinDaily=$proteinDaily " +
                    "fatsDaily=$fatsDaily " +
                    "carbsDaily=$carbsDaily"
        )

        data.caloriesDaily = caloriesDaily
        data.proteinDaily = proteinDaily
        data.fatsDaily = fatsDaily
        data.carbsDaily = carbsDaily
        data.dataReady = true
    }

}